package com.zha.boss.mvc.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zha.boss.dao.entity.PayProduct;
import com.zha.boss.dao.entity.PayWay;
import com.zha.boss.dao.mapper.PayProductMapper;

import com.zha.boss.dao.mapper.PayWayMapper;
import com.zha.boss.dao.result.DicItem;
import com.zha.boss.enums.resp.RespCode;
import com.zha.boss.enums.status.PublicStatus;
import com.zha.boss.exceptions.BossException;
import com.zha.boss.mvc.service.ProductService;
import com.zha.boss.util.NoBuilderUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author: zha
 * @CreateTime: 2024年09月15日 8:45
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Resource
    private PayProductMapper payProductMapper;
    @Resource
    private PayWayMapper payWayMapper;

    @Override
    public void addProduct(String productCode, String productName) {

        //支付productCode是唯一的
        PayProduct payProduct = payProductMapper.selectOne(
                new QueryWrapper<PayProduct>().eq("product_code", productCode));
        if( payProduct != null){
            throw new BossException(RespCode.PAY_PRODUCT_CODE_EXISTS);
        }

        //支付productName是唯一的
        payProduct = payProductMapper.selectOne(new QueryWrapper<PayProduct>()
                .eq("product_name", productName));
        if(payProduct != null){
            throw new BossException(RespCode.PAY_PRODUCT_NAME_EXISTS);
        }

        //创建支付产品
        PayProduct  productRecord = new PayProduct();
        productRecord.setId(NoBuilderUtil.getUUID());
        productRecord.setProductCode(productCode);
        productRecord.setProductName(productName);
        productRecord.setStatus(PublicStatus.ENABLE.name());
        productRecord.setEditTime(new Date());
        productRecord.setRemark("创建支付产品");
        payProductMapper.insert(productRecord);

    }

    @Override
    public IPage<PayProduct> productPageList(Integer pageNo, Integer pageSize) {
        QueryWrapper<PayProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("edit_time");
        return payProductMapper.selectPage(Page.of(pageNo, pageSize), queryWrapper);
    }

    @Override
    public List<DicItem> queryProductDicInfo() {
        List<DicItem> dicItemList = payProductMapper.selectDicList();
        return dicItemList;
    }
    // 删除支付产品 pay_product,pay_way
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void removeProduct(String productCode) {
        // 删除产品表
        QueryWrapper<PayProduct> wrapper = new QueryWrapper<>();
        wrapper.eq("product_code", productCode);
        payProductMapper.delete(wrapper);
        // 删除产品关联的支付方式表
        QueryWrapper<PayWay> payWayWrapper = new QueryWrapper<>();
        payWayWrapper.eq("pay_product_code", productCode);
        payWayMapper.delete(payWayWrapper);
    }

    /**
     * 修改产品状态
     * @param productCode
     */
    @Override
    public void updateProductStatus(String productCode) {
        // 查询支付产品
        QueryWrapper<PayProduct> wrapper = new QueryWrapper<>();
        wrapper.eq("product_code", productCode);
        PayProduct payProduct = payProductMapper.selectOne(wrapper);
        if(payProduct == null){
            throw new BossException(RespCode.PAY_PRODUCT_NONE);
        }
        // 获取产品状态
        String newStatus = payProduct.getStatus();
        // 修改产品状态
        if (PublicStatus.ENABLE.name().equals(payProduct.getStatus())){
            newStatus = PublicStatus.DISABLE.name();
        }
        if (PublicStatus.DISABLE.name().equals(payProduct.getStatus())){
            newStatus = PublicStatus.ENABLE.name();
        }
        UpdateWrapper<PayProduct> updateWrapper = new UpdateWrapper<>();

        updateWrapper.set("status", newStatus).eq("product_code", productCode);
        payProductMapper.update(updateWrapper);
    }

    @Override
    public List<PayProduct> matchProductByName(String productName) {
        QueryWrapper<PayProduct> wrapper = new QueryWrapper<>();
        wrapper.select("product_code", "product_name")
                .like("product_name", productName)
                .orderByAsc("product_name");
        return payProductMapper.selectList(wrapper);
    }
}
